package com.atguigu.gmall;

import org.apache.commons.collections.map.HashedMap;
import org.redisson.misc.Hash;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: 李旭
 * @create: 2021-08-31 16:07
 *
 *    异步编排
 *
 **/
public class CompletableFutureDemo {

    public static void main(String[] args) throws Exception{// 主线程
        //自定义线程池
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(
                        5,//核心线程数
                        10,//最大线程数  5
                        5,//存活时间：超出核心线程数的线程的存活时间
                        TimeUnit.MINUTES,//单位
                        new ArrayBlockingQueue(10) //队列
                );

        //1:创建新的线程 无返回值
        CompletableFuture.runAsync(() -> { //Runnable   只能将异常自己处理 不可以抛出来
            System.out.println("runAsync" + Thread.currentThread().getName());
        },threadPoolExecutor);
        //2:创建新的线程  有返回值
        CompletableFuture<String> stringCompletableFuture =
             CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync" + Thread.currentThread().getName());
            return "我是返回值";
        }, threadPoolExecutor);
        System.out.println(stringCompletableFuture.get());//Callable  将异常抛到主线程中
        //3:创建新的线程  有返回值
        CompletableFuture<HashedMap> hashedMapCompletableFuture =
                CompletableFuture.completedFuture(new HashedMap());
        System.out.println(hashedMapCompletableFuture.get());//HashMap的内容

    }
}
